Basic Python and native data structures: exercises

Define a function max() that takes two numbers as arguments and returns the largest of them.


In [ ]:

Write a function find_longest_word() that takes a list of words and returns the length of the longest one


In [ ]:

Write a function filter_long_words() that takes a list of words and an integer n and returns the list of words that are longer than n.


In [ ]:

Define a function generate_n_chars() that takes an integer n and a character c and returns a string, n characters long, consisting only of the chosen character. For example, generate_n_chars(5,"x") should return the string "xxxxx".


In [ ]:

Write a program that takes list of words and returns a dictionary with the words as keys and their length as values.


In [ ]:

A pangram is a sentence that contains all the letters of the English alphabet at least once, for example: The quick brown fox jumps over the lazy dog. Your task here is to write a function to check a sentence to see if it is a pangram or not.


In [ ]:

"99 Bottles of Beer" is a traditional song in the United States and Canada. It is popular to sing on long trips, as it has a very repetitive format which is easy to memorize, and can take a long time to sing. The song's simple lyrics are as follows:

99 bottles of beer on the wall, 99 bottles of beer.
Take one down, pass it around, 98 bottles of beer on the wall.

The same verse is repeated, each time with one fewer bottle. The song is completed when the singer or singers reach zero.

Your task here is write a function capable of generating all the verses of the song.


In [ ]:

Write a function char_freq() that takes a string and builds a frequency listing of the characters contained in it. Represent the frequency listing as a dictionary. Try it with something like char_freq("abbabcbdbabdbdbabababcbcbab").


In [ ]:

Slightly more difficult ones

Write a function that will calculate the average word length of a text stored in a file (i.e the sum of all the lengths of the word tokens in the text, divided by the number of word tokens). Add an option to exclude blank lines or chapter headers from the computation.

Use the aristotle.txt file contained in the data directory


In [ ]:

If all the previous exercises sounded boring to you

An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; e.g., orchestra = carthorse. Using the word list in the unixdict.txt file (data directory), write a program that finds the sets of words that share the same characters that contain the most words in them.


In [ ]: